package info.liuxing.okhttp.config;

import java.util.concurrent.TimeUnit;

/**
 * config
 *
 * @author liuxing (SE)
 * @summary config
 * @Copyright (c) 2017, Lianjia Group All Rights Reserved.
 * @since 2017-12-04 18:16
 */
public class TimeoutConfig {


    public static final Timeout TIMEOUT_5_SECONDS = Timeout.create(5, 5, 10);
    public static final Timeout TIMEOUT_10_SECONDS = Timeout.create(10, 10, 10);
    public static final Timeout TIMEOUT_30_SECONDS = Timeout.create(30, 30, 10);

    public static class Timeout {
        private int readTimeout;

        private int writeTimeout;

        private int connectTimeout;

        private TimeUnit unit;

        public int getReadTimeout() {
            return readTimeout;
        }

        public void setReadTimeout(int readTimeout) {
            this.readTimeout = readTimeout;
        }

        public int getWriteTimeout() {
            return writeTimeout;
        }

        public void setWriteTimeout(int writeTimeout) {
            this.writeTimeout = writeTimeout;
        }

        public int getConnectTimeout() {
            return connectTimeout;
        }

        public void setConnectTimeout(int connectTimeout) {
            this.connectTimeout = connectTimeout;
        }

        public TimeUnit getUnit() {
            return unit;
        }

        public void setUnit(TimeUnit unit) {
            this.unit = unit;
        }

        /**
         * 构建
         *
         * @param readTimeout    读取超时
         * @param writeTimeout   写入超时
         * @param connectTimeout 连接超时
         * @param unit           单位
         * @return
         */
        public static Timeout create(int readTimeout, int writeTimeout, int connectTimeout, TimeUnit unit) {
            Timeout timeout = new Timeout();
            timeout.setReadTimeout(readTimeout);
            timeout.setWriteTimeout(writeTimeout);
            timeout.setConnectTimeout(connectTimeout);
            timeout.setUnit(unit);
            return timeout;
        }

        /**
         * 构建
         *
         * @param readTimeout    读取超时
         * @param writeTimeout   写入超时
         * @param connectTimeout 连接超时
         * @return
         */
        public static Timeout create(int readTimeout, int writeTimeout, int connectTimeout) {
            Timeout timeout = new Timeout();
            timeout.setReadTimeout(readTimeout);
            timeout.setWriteTimeout(writeTimeout);
            timeout.setConnectTimeout(connectTimeout);
            timeout.setUnit(TimeUnit.SECONDS);
            return timeout;
        }
    }

}
